#include "StdAfx.h"

extern "C"
{
#include "../VocoderBase/vocode.h"
}

#include "time.h"

#include "VocoderStream.h"
#include "ModulatorStream.h"
#include "CarrierStream.h"
#include "Locator.h"
#include "SampleBuffer.h"
#include "WaveSample.h"




VocoderStream::VocoderStream(void)
{
}

VocoderStream::~VocoderStream(void)
{
}

void VocoderStream::Initialize(unsigned int ChannelsCount, unsigned int SampleRate, int windowLength)
{
	sf::SoundStream::Initialize(ChannelsCount, SampleRate);
	vocode_window_length = windowLength;
	set_default_max_magnitude();
	//vocode_normalize = 1;
	//vocode_band_count = 128;
	allocate_memory2();
}

bool VocoderStream::OnStart()
{
	//int len = WINLEN;//vocodeWindow->GetWindowLength();
	//Locator::Instance()->GetModulatorStream()->SetChunkSize(len);
	//Locator::Instance()->GetCarrierStream()->SetChunkSize(len);
	return true;
}


static int totalGetDataTime = 0;
static int countGetData = 0;
static Sample* lastSample = NULL;
bool VocoderStream::OnGetData(Chunk& Data)
{
	clock_t start, finish;
	start = clock();
	if(lastSample!=NULL) delete lastSample;

	Sample* s1 = Locator::Instance()->GetVocoderBuffer()->Read();
	Sample* s2 = Locator::Instance()->GetVocoderBuffer()->Read();

	lastSample = s1->Append(s2);
	Data.NbSamples = lastSample->length;
	Data.Samples = lastSample->data;
	delete s1;
	delete s2;

	finish = clock();
	int time = finish-start;//(double)CLOCKS_PER_SEC;
	countGetData++;
	totalGetDataTime += time;

	return true;
}
